//
//  DrawingUtilities.m
//  turfwars
//
//  Created by Jim Heising on 1/10/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "DrawingUtilities.h"

#define AQUA_HILITE_FADE_ALPHA 0.25

#define UIColorFromRGB(rgbValue) [UIColor \
		colorWithRed :( (float)(rgbValue & 0xFF))/255.0 \
		green :( (float)((rgbValue & 0xFF00) >> 8))/255.0 \
		blue :( (float)((rgbValue & 0xFF0000) >> 16))/255.0 \
		alpha :( (float)((rgbValue & 0xFF000000) >> 24))/255.0]

@implementation DrawingUtilities

+(void)drawShadow:(CGContextRef)context :(CGRect)rect :(float)offsetX :(float)offsetY :(int)distance :(int)cornerRadius
{
	CGRect shadowRect = rect;
	
	CGContextSaveGState(context);
	
	CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
	CGContextSetShadowWithColor(context, CGSizeMake(-offsetX, -offsetY), distance, [UIColor blackColor].CGColor);

	[DrawingUtilities createRoundedRectPath:context :shadowRect :cornerRadius];
	CGContextFillPath(context);
	[DrawingUtilities createRoundedRectPath:context :shadowRect :cornerRadius];
	CGContextFillPath(context);
	
	CGContextRestoreGState(context);
	
	[DrawingUtilities createRoundedRectPath:context :shadowRect :cornerRadius];
	CGContextClip(context);
	CGContextClearRect(context, shadowRect);
}

+(void)drawAquaView: (CGContextRef)context :(CGRect)rect :(UIColor*)backgroundColor :(UIColor*)hiliteColor :(UIColor*)borderColor :(int)borderWidth :(int)cornerRadius :(float)hiliteFillPercentage :(BOOL)drawRounded :(BOOL)drawReversed
{
	CGRect innerRect = CGRectInset(rect, ((float)borderWidth / 2.0), ((float)borderWidth / 2.0));
	
	CGContextClearRect(context, rect);
	
	[DrawingUtilities createRoundedRectPath:context :innerRect :cornerRadius];
	CGContextSetFillColorWithColor(context, backgroundColor.CGColor);
	CGContextFillPath(context);
	
	//CGContextFillRect(context, CGRectInset(rect, (borderWidth), (borderWidth)));
	
	if(drawReversed)
	{
		CGContextSaveGState(context);
		CGContextScaleCTM(context, 1.0, -1.0);
		CGContextTranslateCTM(context, 0.0, -rect.size.height);
	}
	
	CGContextSaveGState(context);
	[DrawingUtilities createRoundedRectPath:context :innerRect :cornerRadius];
	CGContextClip(context);
	
	if(drawRounded)
		[DrawingUtilities drawRoundedAquaOverlayWithColor:context :innerRect :hiliteColor :hiliteFillPercentage];
	else
		[DrawingUtilities drawAquaOverlayWithColor:context :innerRect :hiliteColor :hiliteFillPercentage];
	
	CGContextRestoreGState(context);
	
	if(drawReversed)
	{
		CGContextRestoreGState(context);
	}
	
	if(borderWidth != 0)
	{
		CGContextSetStrokeColorWithColor(context, borderColor.CGColor);
		CGContextSetLineWidth(context, borderWidth);
		[DrawingUtilities createRoundedRectPath:context :innerRect :cornerRadius * 1.0];
		CGContextStrokePath(context);
	}
}

+(void)drawAquaOverlay: (CGContextRef)contextRef :(CGRect)rect
{
	[DrawingUtilities drawAquaOverlayWithAlpha:contextRef :rect :1.0];
}

+(void)drawAquaOverlayWithColor: (CGContextRef)contextRef :(CGRect)rect :(UIColor*)hiliteColor :(float)hiliteFillPercentage
{
	CGGradientRef gradient;
	
	CGColorSpaceRef colorspace;
	
	size_t num_locations = 2;
	CGFloat locations[2] = { 0.0, 1.0 };
	
	const CGFloat* componentsList = CGColorGetComponents(hiliteColor.CGColor);
	
	colorspace = CGColorSpaceCreateDeviceRGB();
	
	if(CGColorSpaceGetNumberOfComponents(CGColorGetColorSpace(hiliteColor.CGColor)) == 1)
	{
		CGFloat components[8] = { componentsList[0], componentsList[0], componentsList[0], componentsList[1],  // Start color
		componentsList[0], componentsList[0], componentsList[0], componentsList[1] * AQUA_HILITE_FADE_ALPHA }; // End color
		
		gradient = CGGradientCreateWithColorComponents (colorspace, components, locations, num_locations);
	}
	else
	{
		CGFloat components[8] = { componentsList[0], componentsList[1], componentsList[2], componentsList[3],  // Start color
		componentsList[0], componentsList[1], componentsList[2], componentsList[3] * AQUA_HILITE_FADE_ALPHA }; // End color
		
		gradient = CGGradientCreateWithColorComponents (colorspace, components, locations, num_locations);
	}
	
	CGContextDrawLinearGradient(contextRef, gradient, CGPointMake(0.0, 0.0), CGPointMake(0.0, (int)(rect.size.height * hiliteFillPercentage)), 0);
	CGGradientRelease(gradient);
}

+(void)drawRoundedAquaOverlayWithColor: (CGContextRef)contextRef :(CGRect)rect :(UIColor*)hiliteColor :(float)hiliteFillPercentage
{
	CGGradientRef gradient;
	
	CGColorSpaceRef colorspace;
	
	size_t num_locations = 2;
	CGFloat locations[2] = { 0.0, 1.0 };
	
	const CGFloat* componentsList = CGColorGetComponents(hiliteColor.CGColor);
	
	colorspace = CGColorSpaceCreateDeviceRGB();
	
	if(CGColorSpaceGetNumberOfComponents(CGColorGetColorSpace(hiliteColor.CGColor)) == 1)
	{
		CGFloat components[8] = { componentsList[0], componentsList[0], componentsList[0], componentsList[1],  // Start color
		componentsList[0], componentsList[0], componentsList[0], componentsList[1] * AQUA_HILITE_FADE_ALPHA }; // End color
		
		gradient = CGGradientCreateWithColorComponents (colorspace, components, locations, num_locations);
	}
	else
	{
		CGFloat components[8] = { componentsList[0], componentsList[1], componentsList[2], componentsList[3],  // Start color
		componentsList[0], componentsList[1], componentsList[2], componentsList[3] * AQUA_HILITE_FADE_ALPHA }; // End color
		
		gradient = CGGradientCreateWithColorComponents (colorspace, components, locations, num_locations);
	}
	
	CGContextSaveGState(contextRef);
	
	CGRect ellipseRect = rect;
	
	ellipseRect.origin.y = -(int)(rect.size.height * hiliteFillPercentage);
	ellipseRect.size.height = (int)(rect.size.height * hiliteFillPercentage * 2.0);
	ellipseRect.size.width *= 1.5;
	ellipseRect.origin.x = (rect.size.width - ellipseRect.size.width) / 2.0;
	
	CGContextBeginPath(contextRef);
	CGContextAddEllipseInRect(contextRef, ellipseRect);
	CGContextClosePath(contextRef);
	
	CGContextClip(contextRef);
	
	CGContextDrawLinearGradient(contextRef, gradient, CGPointMake(0.0, 0.0), CGPointMake(0.0, (int)(rect.size.height * hiliteFillPercentage)), 0);
	
	CGGradientRelease(gradient);
	
	CGContextRestoreGState(contextRef);
}

+(void)drawAquaOverlayWithAlpha: (CGContextRef)contextRef :(CGRect)rect :(float)alpha
{
	[DrawingUtilities drawAquaOverlayWithColor:contextRef :rect :[UIColor colorWithWhite:1.0 alpha:alpha] :0.5];
	
	/*CGGradientRef gradient;
	
	CGColorSpaceRef colorspace;
	
	size_t num_locations = 2;
	CGFloat locations[2] = { 0.0, 1.0 };
	CGFloat components[8] = { 1.0, 1.0, 1.0, 0.25 * alpha,  // Start color
	1.0, 1.0, 1.0, 0.09 * alpha }; // End color
	
	
	colorspace = CGColorSpaceCreateDeviceRGB(); //CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
	
	gradient = CGGradientCreateWithColorComponents (colorspace, components, locations, num_locations);
	
	CGContextDrawLinearGradient(contextRef, gradient, CGPointMake(0.0, 0.0), CGPointMake(0.0, CGRectGetMidY(rect)), 0);
	CGGradientRelease(gradient);*/
}

+(void)drawRoundedAquaOverlayWithAlpha: (CGContextRef)contextRef :(CGRect)rect :(float)alpha
{
	[DrawingUtilities drawRoundedAquaOverlayWithColor:contextRef :rect :[UIColor colorWithWhite:1.0 alpha:alpha] :0.5];

	/*CGGradientRef gradient;
	
	CGColorSpaceRef colorspace;
	
	size_t num_locations = 2;
	CGFloat locations[2] = { 0.0, 1.0 };
	CGFloat components[8] = { 1.0, 1.0, 1.0, 0.25 * alpha,  // Start color
	1.0, 1.0, 1.0, 0.09 * alpha }; // End color
	
	CGContextSaveGState(contextRef);
	
	CGRect ellipseRect = rect;
	
	ellipseRect.origin.y = -ellipseRect.size.height / 2.0;
	ellipseRect.size.width *= 1.5;
	ellipseRect.origin.x = (rect.size.width - ellipseRect.size.width) / 2.0;
	
	CGContextBeginPath(contextRef);
	CGContextAddEllipseInRect(contextRef, ellipseRect);
	CGContextClosePath(contextRef);
	
	CGContextClip(contextRef);
	
	colorspace = CGColorSpaceCreateDeviceRGB();
	
	gradient = CGGradientCreateWithColorComponents (colorspace, components, locations, num_locations);
	
	CGContextDrawLinearGradient(contextRef, gradient, CGPointMake(0.0, 0.0), CGPointMake(0.0, CGRectGetMidY(rect)), 0);
	
	CGGradientRelease(gradient);
	
	CGContextRestoreGState(contextRef);*/
}

+(UIColor*)uiColorFromHexColorString:(NSString*)hexString
{
	NSScanner* stringScanner = [NSScanner scannerWithString:hexString];
	
	unsigned long long longValue = 0;
	
	BOOL result = [stringScanner scanHexLongLong:&longValue];
	
	return UIColorFromRGB(longValue);
}

+(void)createRoundedRectPath:(CGContextRef)contextRef :(CGRect)rect :(float)cornerRadius
{
	CGContextBeginPath(contextRef);
	
	CGContextMoveToPoint(contextRef, CGRectGetMinX(rect) + cornerRadius, CGRectGetMinY(rect));
	
	CGContextAddArc(contextRef, CGRectGetMaxX(rect) - cornerRadius, CGRectGetMinY(rect) + cornerRadius, cornerRadius, 3 * M_PI / 2, 0, 0);
	CGContextAddArc(contextRef, CGRectGetMaxX(rect) - cornerRadius, CGRectGetMaxY(rect) - cornerRadius, cornerRadius, 0, M_PI / 2, 0);
	
	CGContextAddArc(contextRef, CGRectGetMinX(rect) + cornerRadius, CGRectGetMaxY(rect) - cornerRadius, cornerRadius, M_PI / 2, M_PI, 0);
	CGContextAddArc(contextRef, CGRectGetMinX(rect) + cornerRadius, CGRectGetMinY(rect) + cornerRadius, cornerRadius, M_PI, 3 * M_PI / 2, 0);
	
	CGContextClosePath(contextRef);
}

@end
